home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / ask_man.h < prev    next >
C/C++ Source or Header  |  1994-10-10  |  5KB  |  137 lines

  1. #ifndef __ASK_MANAGER_H_
  2. #define __ASK_MANAGER_H_
  3.  
  4. #include "pxtable.h"
  5. #include "khpxeerr.h"
  6. #include "strtable.h"
  7. #include "khquerry.h"
  8.  
  9. extern int indexNo;  // The only reason - error in PX Engine.
  10.  
  11. /* AskManager works with list of querries and corresponding list of
  12. tables. Table names in list may be not unique.
  13.  
  14.     The manager works in the following manner:
  15.     ( ATTENTION !!! NO OPTIMIZATION REFLECTED - IT IS EXPLANATION ONLY )
  16.     a. Verification.
  17.     EXAMPLES. Second table MUST contain common variable(s)
  18.     with 1-st, third - with the 1-st or 2-nd and so on. If this
  19.     condition fails, querry fails too, because one of tables is not
  20.     linked with others.
  21.     CHECKERS. At least one checked field should be present.
  22.  
  23.     b. Maketing.
  24.     ANSWER table is created as combination of all checked
  25.     fields in all tables. If there are the same field names,
  26.     their names are modified: NAME, NAME_1 and so on.
  27.  
  28.     c. If there are only one table in querry.
  29.     For every record call in cycle Find() function, copying results
  30.     to ANSWER.
  31.  
  32.  
  33.     After processing of this querry we get the table of EXAMPLE values
  34.     and checked fields values. In the general case number of
  35.     records in this table is less than in 1-st table (if CONDITIONs
  36.     are not always TRUE).
  37.  
  38.     d. Second pass.
  39.     We need only the data from second table which EXAMPLE field
  40.     contains the same value as in 1-st table. In general, we should
  41.     for EVERY record in WORK table
  42.         for EVERY record in 2-nd table
  43.         test if(EXAMPLE field (1) == EXAMPLE field (2) )
  44.  
  45.     Really, having 1.000 records (WORK) and 1.000 records (2), we
  46.     need 1.000.000 passes through 2-nd table !!!
  47.  
  48.     The solution is: test more than one line of WORK in every pass, or
  49.     (the same result) to allocate the largest possible buffer for
  50.     keepeng 2-nd table in memory.
  51.  
  52.     AskManager builds the table, containing checked fields for
  53.     examples (adding if necessary new example names from 2-nd table),
  54.     checked fields for fields, containing CONDITIONS (we assume Address
  55.     is the 2-nd table field with CONDITION):
  56.  
  57.     Name:               Address:
  58.         _________________________________________________
  59.     EXAMPLE, CHECKPLUS  EXAMPLE, CHECKPLUS
  60.  
  61.     and fill it with field numbers of 1-st and 2-nd tables, for
  62.     which EXAMPLE values are the same.
  63.  
  64.     The real algorythm is:
  65.  
  66.     Get 1-st record from 2-nd table. Check condition (if present),
  67.         if failed, get next record (using Find() and values of examples).
  68.     If condition is TRUE, for all fields in WORK table (for which
  69.         the largest buffer is allocated) find the record with the same
  70.         example(s) values, as in the record from 2-nd table.
  71.         Attention!!! We have already check conditions in WORK table.
  72.     Now we have two records, from 1-st table and 2-nd table, with
  73.         common EXAMPLE values. For all checked but not containing
  74.         EXAMPLES fields with the same names (if no such fields - no
  75.         checking) test, if their values are the same.
  76.     Fill the ANSWER table. If the table was not last in querry -
  77.         rename it to WORK.
  78.  
  79.     Repeate operation for all tables in querry.
  80. */
  81. /*   ATTENTION !!! We don't check, if BLOB fields contains conditions
  82.      or checkers. Do it !!!
  83. */
  84. class KH_AskManager
  85.     {
  86.     protected:
  87.     KH_PXTable** tables;              // List of tables
  88.     KH_QUERRY** querryList;           // There are 1-to-1 correspondence
  89.                      //   between tables and querries
  90.     KH_PXTable* workTable;            // We use this table as temporary
  91.                      //   to keep sub-results
  92.     KH_PXTable* answerTable;               // Resulting table
  93.     int numberOfTables;              // Total number of tables in "Ask"
  94.  
  95.     public:
  96. // Constructor. Arguments: names of tables, querries, number of tables
  97.     KH_AskManager(char** tableNames, KH_QUERRY** querries, int num);
  98.  
  99. /*  Destructor. Closes tables with KH_SHARE flag == 1, reduces this flag
  100.     for other tables. Removes WORK and WORK1 tables.
  101. */
  102.     ~KH_AskManager();
  103. /*  Service (non-user) function. Creates ANSWER.DB table containing all
  104.     checked fields from all querries. Return value: 0 (fail), 1 (success).
  105.     The enumeration KH_CHECKERS could include CALC (not in 1.0).
  106.     This option is used to produce new field with the same type as in
  107.     source field, and value which is calculated: x, CALC 3 * x.
  108.     In this string we define EXAMPLE variable x, and forse the AskManager
  109.     to create new column. x have the value of current field, and field
  110.     value in new column will be 3 * x. createTable() considers CALC
  111.     as CHECK and include new column to ANSWER.
  112. */
  113.     int createAnswerTable(int nOT);
  114. /*  checkQuerry() tests the querry BEFORE processing. It may be virtual,
  115.     because user could redefine the verification procedure.
  116. */
  117.     /*virtual*/ int KH_AskManager::checkQuerry();
  118.     KH_STRTABLE* getExamples(int n);
  119.     KH_STRTABLE* getCurrExamples(int n);
  120.  
  121.     KH_STRTABLE* makeQuerry(int n);  // Produce complete querry from packed one
  122.     int processSingleTable();  // If there are 1 table in querry
  123.     int processMultipleTables();
  124.     int process();             // Process the querry
  125.     int moveToRec(int* ex, int cur, int n, int m, FIELDHANDLE cSIHandle);
  126.     int getExSrc(KH_STRTABLE* ex, KH_STRTABLE* cur, int* eNums,
  127.              FIELDHANDLE* exFlds);
  128.  
  129.     int getExFields(FIELDHANDLE* exFlds, int i);
  130.     void writeField(KH_PXTable* srcTable, KH_PXTable* destTable);
  131.     void dumpAnswer();
  132.  
  133.     };
  134.  
  135. #endif __ASK_MANAGER_H_
  136.  
  137.